﻿Public Class FormAdapter(Of F As Form, L As FormListener)

    Protected WithEvents form As F

    Private observable As New Observable(Of L)

    Public Sub New(ByRef newForm As F)
        form = newForm
    End Sub

    Public Sub show()
        form.Show()
        form.Focus()
        If form.WindowState = FormWindowState.Minimized Then
            form.WindowState = FormWindowState.Normal
        End If
    End Sub

    Public Sub hide()
        form.Hide()
    End Sub

    Public Sub setEnabled(ByRef enabled As Boolean)
        form.Enabled = enabled
    End Sub

    Public Function getAsFormAdapter() As FormAdapter(Of Form, FormListener)
        Return New FormAdapter(Of Form, FormListener)(form)
    End Function

    Public Sub showError(ByRef message As String)
        form.Enabled = False
        MsgBox(message, MsgBoxStyle.Critical, "Fehler")
        form.Enabled = True
    End Sub

    Public Overridable Sub form_onClose(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles form.FormClosing
        For Each listener As L In observable.getListeners
            listener.onClose()
        Next
        e.Cancel = True
    End Sub

    ' sets the default values for the form.
    Public Overridable Sub form_onShown() Handles form.Shown
        'defaults for form
        'form.StartPosition = FormStartPosition.Manual
        'form.Location = DEFAULT_LOCATION
    End Sub

    Public Function getObservable() As Observable(Of L)
        Return observable
    End Function
End Class
